package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Address;
import com.itheima.pojo.Setmeal;
import com.itheima.service.AddressService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.UUID;

@RestController
@RequestMapping("/address")
public class AddressController {
    @Autowired private JedisPool jedisPool;
    @Reference private AddressService addressService;

    /**
     * 图片上传
     * @param imgFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        Jedis resource = null;
        try {
            String originalFilename = imgFile.getOriginalFilename();
            int indexOf = originalFilename.lastIndexOf(".");
            String suffix = originalFilename.substring(indexOf-1);
            //获取不可重复的文件名
            String fileName = UUID.randomUUID().toString() + suffix;
            //将所有文件保存到Redis集合
            resource = jedisPool.getResource();
            resource.sadd(RedisConstant.ADDRESS_PIC_RESOURCES, fileName);
            //图片存到七牛云
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);

            //文件上传成功
            return  new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, fileName);
        } catch (Exception e) {
            e.printStackTrace();
            //文件上传失败
            Result result = new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
            return result;
        } finally {
            //释放资源
            if (resource != null){
                resource.close();
            }
        }
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage.do")
    @PreAuthorize("hasAuthority('ADDRESS_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = addressService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * 新增机构地址
     * @param address
     * @return
     */
    @RequestMapping("/add.do")
    @PreAuthorize("hasAuthority('ADDRESS_ADD')")
    public Result add(@RequestBody Address address){
        try {
            addressService.add(address);
            return new Result(true,"新增机构地址成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"新增机构地址失败");
        }
    }

    /**
     * 编辑机构地址
     * @param address
     * @return
     */
    @RequestMapping("/edit.do")
    @PreAuthorize("hasAuthority('ADDRESS_EDIT')")
    public Result edit(@RequestBody Address address){
        try{
            addressService.edit(address);
            return new Result(true,"修改机构地址成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"修改机构地址失败");
        }
    }

    @RequestMapping("/deleteById.do")
    @PreAuthorize("hasAuthority('ADDRESS_DELETE')")
    public Result deleteById(Integer id){
        try{
            addressService.deleteById(id);
            return new Result(true,"删除机构地址成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"无法删除，该地址已被预约");
        }
    }
}
